package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.AdminMessage;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Teacher;
import com.woniuxy.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * (Teacher)表控制层
 *
 * @author zjx
 * @since 2021-05-14 19:42:03
 */
@RestController
@CrossOrigin
@RequestMapping("teacher")
@Api(value = "讲师接口",description = "用于对讲师信息实现CRUD")
public class TeacherController {
    /**
     * 服务对象
     */
    @Resource
    private TeacherService teacherService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据讲师id查询讲师信息")
    @GetMapping("selectOne/{id}")
    public ResponseEntity selectOne(@PathVariable @ApiParam("讲师id") Integer id) {
        Teacher teacher = this.teacherService.queryById(id);
        return new ResponseEntity(200,"查询单个讲师信息成功",teacher);
    }

    /**
     * 分页查询
     *
     * @param map 封装了分页信息和讲师信息
     *
     * @return 分页信息
     */
    @ApiOperation("查询所有并分页:传入map")
    @PostMapping("selectAll")
    public ResponseEntity selectAll(@RequestBody Map<String,Object> map){
        int startPage = (int) map.get("page");
        int pageSize = (int) map.get("limit");
        JSONObject jsonObject=JSONObject.fromObject(map.get("teacher"));
        Teacher teacher = (Teacher)JSONObject.toBean(jsonObject, Teacher.class);
        PageInfo<Teacher> pageInfo = this.teacherService.queryAllPage(startPage,pageSize,teacher);
        System.out.println("分页信息"+pageInfo.getList());
        //此处有坑,若删掉的信息为当前页最后一条,此时前端将查不到数据
        if(pageInfo.getList().size()==0&&startPage>1){
            pageInfo = this.teacherService.queryAllPage(--startPage,pageSize,teacher);
        }
        return new ResponseEntity(200,"分页查询讲师信息成功",pageInfo);
    }
    /**
     * 新增一条讲师信息
     *
     * @param teacher 讲师信息
     * @return 讲师信息
     */
    @ApiOperation("新增一条讲师信息:传入讲师对象")
    @PostMapping("addTeacher")
    public ResponseEntity addTeacher(@RequestBody Teacher teacher){
        String result = this.teacherService.insert(teacher);
        return new ResponseEntity(200, result, null);
    }

    /**
     * 通过id删除讲师信息
     *
     * @param id 讲师id
     * @return  布尔值,是否成功
     */
    @ApiOperation("删除讲师信息:传入讲师id")
    @GetMapping("deleteTeacher/{id}")
    public ResponseEntity deleteTeacher(@ApiParam("讲师id")@PathVariable int id){
        String result = this.teacherService.deleteById(id);
        return new ResponseEntity(200, result, null);
    }

    /**
     * 通过传入的讲师信息实体类修改信息,并返回修改后的讲师信息实体
     *
     * @param teacher 讲师信息实体类
     * @return 讲师信息实体类
     */
    @ApiOperation("修改讲师信息:传入讲师对象")
    @PostMapping(value = "updateTeacher")
    public ResponseEntity updateTeacher(@RequestBody Teacher teacher){
        String result = this.teacherService.update(teacher);
        return new ResponseEntity(200, result, null);
    }
    @ApiOperation("查询所有讲师标签")
    @GetMapping(value = "selectLabels")
    public ResponseEntity selectLabels(){
        List<String> labels = this.teacherService.selectLabels();
        if(labels.size()>0){
            return new ResponseEntity(200, "查询所有标签成功", labels);
        }else{
        return new ResponseEntity(404, "查询所有标签失败", null);
        }
    }

    @ApiOperation("上传文件")
    @PostMapping(value = "upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String uploadFile(HttpServletRequest request,
                             @RequestParam("file") MultipartFile multipartFile, ModelMap modelMap){
        //存文件路径! 存别人的图片服务! 七牛云
        //C:\Users\mayuhang\Desktop\third\spring09\springmvc-fileup\web\file\30道项目问题.md
        String filePath=request.getServletContext().getRealPath("/file/")+
                File.separator+multipartFile.getOriginalFilename();
        File file = new File(filePath);
        //判断父目录 file文件夹是否存在
        if (!file.getParentFile().exists()) {
            //如果没有就创建一个
            file.getParentFile().mkdirs();
        }
        try {
            //存文件!
            multipartFile.transferTo(file);
            //往数据库存入数据 调用service
            //第一个 存全路径
            //第二个 存文件名
            System.out.println(filePath);
            modelMap.addAttribute("context",filePath);
            return "success";
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }
    }
}
